package com.wnxy.pawnbackgroundsystem.commons;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class ServletApiUtil {

    public static HttpServletRequest getRequest(){
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }
    public static HttpServletResponse getResponse(){
        return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse();
    }
    public static HttpSession getSession(){
        return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest().getSession();
    }
    public static void setSession(String key ,Object data){
        getSession().setAttribute(key,data);
    }
    public static <T> T getSessionValue(Class<T> t,String key){
        Object attribute = getSession().getAttribute(key);
        return (T)attribute;
    }
    public static void setRequestAttr(String key ,Object data){
        getRequest().setAttribute(key,data);
    }
    public static String getRequestHander(String handName){
        String header = getRequest().getHeader(handName);
        return header;
    }
    public static void setResponseHander(String headName,String headVaule){
        getResponse().setHeader(headName,headVaule);
    }
}
